package com.sviet.game.tetriswindow;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.google.ads.mediation.admob.AdMobAdapterExtras;
import com.sviet.game.tetriswindow.TetrisTileView.Navigations;
import com.sviet.game.tetriswindow.data.GlobalVariable;
import com.sviet.game.tetriswindow.dialog.NewHighScoreDialog;
import com.sviet.game.tetriswindow.listener.ToastAdListener;
import com.sviet.game.tetriswindow.util.Constants;
import com.sviet.game.tetriswindow.util.SharedPreferencesUtils;

public class TetrisActivity extends Activity implements OnClickListener,
		OnDismissListener, OnLongClickListener{

	private String TAG = "Tetris";
	private TetrisTileView mTetrisView;
	// private TetrisTileView mTetrisView1;

	private ImageButton imgbtn_left;
	private ImageButton imgbtn_up;
	private ImageButton imgbtn_right;
	private ImageButton imgbtn_down;
	private ImageButton imgbtn_play_stop;
	private Button bt_new_game;
	private Button bt_menu;
	private TetrisHudView mTetrisHudView;
	private Button bt_play_stop;
	private Button bt_exit;
	private FrameLayout fl_control_navigation;
	private FrameLayout fl_control_touch;
	private Button bt_menu1;
	private Button bt_new_game1;
	private NewHighScoreDialog dialog;
	private AdView ad_view;
//	private View imgbtn_close_ad;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tetris);

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		GlobalVariable.scaledDensity = metrics.scaledDensity;

		mTetrisView = (TetrisTileView) findViewById(R.id.tetris_tile_view);
		// mTetrisView1 = (TetrisTileView) findViewById(R.id.tetris_tile_view1);

		mTetrisHudView = (TetrisHudView) findViewById(R.id.tetris_hud_view);

		mTetrisView.setTetrisHudView(mTetrisHudView);

		imgbtn_left = (ImageButton) findViewById(R.id.imgbtn_left);
		imgbtn_left.setOnClickListener(this);
		imgbtn_up = (ImageButton) findViewById(R.id.imgbtn_up);
		imgbtn_up.setOnClickListener(this);
		imgbtn_right = (ImageButton) findViewById(R.id.imgbtn_right);
		imgbtn_right.setOnClickListener(this);
		imgbtn_down = (ImageButton) findViewById(R.id.imgbtn_down);
		imgbtn_down.setOnClickListener(this);
		imgbtn_down.setOnLongClickListener(this);
		imgbtn_play_stop = (ImageButton) findViewById(R.id.imgbtn_play_stop);
		imgbtn_play_stop.setOnClickListener(this);
		bt_new_game = (Button) findViewById(R.id.bt_new_game);
		bt_new_game.setOnClickListener(this);
		bt_menu = (Button) findViewById(R.id.bt_menu);
		bt_menu.setOnClickListener(this);

		bt_new_game1 = (Button) findViewById(R.id.bt_new_game1);
		bt_new_game1.setOnClickListener(this);
		bt_menu1 = (Button) findViewById(R.id.bt_menu1);
		bt_menu1.setOnClickListener(this);

		bt_play_stop = (Button) findViewById(R.id.bt_play_stop);
		bt_play_stop.setOnClickListener(this);
		bt_exit = (Button) findViewById(R.id.bt_exit);
		bt_exit.setOnClickListener(this);

		fl_control_navigation = (FrameLayout) findViewById(R.id.tetris_fl_control_navigation);
		fl_control_touch = (FrameLayout) findViewById(R.id.tetris_fl_control_touch);

		dialog = new NewHighScoreDialog(this);
		dialog.setOnDismissListener(this);
		mTetrisView.setNewHighScoreDialog(dialog);
		
		ad_view = (AdView) findViewById(R.id.tetris_ad_view);
		// ad_view.setAdUnitId(getResources().getString(R.string.ad_unit_id));
		// ad_view.setAdSize(AdSize.BANNER);
		ad_view.setAdListener(new ToastAdListener(this));
		AdRequest adRequest = new AdRequest();
		AdMobAdapterExtras extras = new AdMobAdapterExtras()
				.addExtra("color_bg", "0099FF")
				.addExtra("color_bg_top", "FFFFFF")
				.addExtra("color_border", "FFFFFF")
				.addExtra("color_link", "000080")
				.addExtra("color_text", "FFFFFF")
				.addExtra("color_url", "008000");

		adRequest.setNetworkExtras(extras);
		ad_view.loadAd(adRequest);

//		imgbtn_close_ad = (ImageButton) findViewById(R.id.tetris_imgbtn_close_ad);
//		imgbtn_close_ad.setOnClickListener(this);

		// mTetrisView.setMode(TetrisTileView.RUNNING);

		// mStartButton = (Button) findViewById(R.id.start_button);
		// mStartButton.setFocusable(false);
		// mPauseButton = (Button) findViewById(R.id.pause_button);
		// mPauseButton.setFocusable(false);
		// mPauseButton.setVisibility(View.INVISIBLE);
		// mResumeButton = (Button) findViewById(R.id.resume_button);
		// mResumeButton.setFocusable(false);
		// mResumeButton.setVisibility(View.INVISIBLE);
		// mResetButton = (Button) findViewById(R.id.reset_button);
		// mResetButton.setFocusable(false);
		// mResetButton.setVisibility(View.INVISIBLE);

		// mStartButton.setOnClickListener(new View.OnClickListener() {
		// public void onClick(View view) {
		// view.setVisibility(View.INVISIBLE);
		// mPauseButton.setVisibility(View.VISIBLE);
		// mResetButton.setVisibility(View.VISIBLE);
		//
		// mTetrisView.setMode(TetrisTileView.RUNNING);
		// // mTetrisView1.setMode(TetrisTileView.RUNNING);
		// }
		// });
		// mPauseButton.setOnClickListener(new View.OnClickListener() {
		// public void onClick(View view) {
		// view.setVisibility(View.INVISIBLE);
		// mResumeButton.setVisibility(View.VISIBLE);
		// mResetButton.setVisibility(View.VISIBLE);
		//
		// mTetrisView.setMode(TetrisTileView.PAUSE);
		// // mTetrisView1.setMode(TetrisTileView.PAUSE);
		// }
		// });
		// mResumeButton.setOnClickListener(new View.OnClickListener() {
		// public void onClick(View view) {
		// view.setVisibility(View.INVISIBLE);
		// mPauseButton.setVisibility(View.VISIBLE);
		//
		// mTetrisView.setMode(TetrisTileView.RUNNING);
		// // mTetrisView1.setMode(TetrisTileView.RUNNING);
		// }
		// });
		// mResetButton.setOnClickListener(new View.OnClickListener() {
		// public void onClick(View view) {
		// view.setVisibility(View.INVISIBLE);
		// mStartButton.setVisibility(View.VISIBLE);
		// mResumeButton.setVisibility(View.INVISIBLE);
		// mPauseButton.setVisibility(View.INVISIBLE);
		//
		// TetrisTileView.mPlayerCount = 0;
		// TetrisTileView.mFinishedPlayerCount = 0;
		//
		// mTetrisView.setMode(TetrisTileView.READY);
		// // mTetrisView1.setMode(TetrisTileView.READY);
		// }
		// });
		//
		// TetrisTileView.setButtons(mResetButton, mPauseButton);

		if (savedInstanceState == null) {
			// We were just launched -- set up a new game
			mTetrisView.setMode(TetrisTileView.READY);
			int control_mode = SharedPreferencesUtils.GetPreference(this,
					SharedPreferencesUtils.CONTROL_MODE);
			mTetrisView.setControlMethod(control_mode);
			// mTetrisView1.setMode(TetrisTileView.READY);
			// mTetrisView1.setControlMethod(TetrisTileView.CONTROL_KEYBOARD);

			if (control_mode == TetrisTileView.CONTROL_NAVIGATION) {
				fl_control_navigation.setVisibility(View.VISIBLE);
				fl_control_touch.setVisibility(View.GONE);
			} else if (control_mode == TetrisTileView.CONTROL_TOUCH) {
				fl_control_navigation.setVisibility(View.GONE);
				fl_control_touch.setVisibility(View.VISIBLE);
			}
		} else {
			// We are being restored

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == Constants.EXITAPP) {
			if (resultCode == RESULT_OK) {
				finish();
			}
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		int control_mode = SharedPreferencesUtils.GetPreference(this,
				SharedPreferencesUtils.CONTROL_MODE);
		Log.v(TAG, "control_mode = " + control_mode);
		mTetrisView.setControlMethod(control_mode);
		if (control_mode == TetrisTileView.CONTROL_NAVIGATION) {
			fl_control_navigation.setVisibility(View.VISIBLE);
			fl_control_touch.setVisibility(View.GONE);
		} else if (control_mode == TetrisTileView.CONTROL_TOUCH) {
			fl_control_navigation.setVisibility(View.GONE);
			fl_control_touch.setVisibility(View.VISIBLE);
		}

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		if (mTetrisView.getMode() == TetrisTileView.RUNNING) {
			GlobalVariable.stop_time = System.currentTimeMillis();
			mTetrisView.setMode(TetrisTileView.PAUSE);
			imgbtn_play_stop.setImageResource(R.drawable.stopping_btn);
			bt_play_stop.setText(getString(R.string.play));
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Log.v(TAG, "onClick");
		if (v.getId() == R.id.imgbtn_left) {
			mTetrisView.navigationUpdate(Navigations.LELT);
		} else if (v.getId() == R.id.imgbtn_up) {
			mTetrisView.navigationUpdate(Navigations.UP);
		} else if (v.getId() == R.id.imgbtn_right) {
			mTetrisView.navigationUpdate(Navigations.RIGHT);
		} else if (v.getId() == R.id.imgbtn_down) {
			mTetrisView.navigationUpdate(Navigations.DOWN);
		} else if (v.getId() == R.id.imgbtn_play_stop
				|| v.getId() == R.id.bt_play_stop) {
			if (mTetrisView.getMode() == TetrisTileView.RUNNING) {
				GlobalVariable.stop_time = System.currentTimeMillis();
				mTetrisView.setMode(TetrisTileView.PAUSE);
				imgbtn_play_stop.setImageResource(R.drawable.stopping_btn);
				bt_play_stop.setText(getString(R.string.play));
			} else if (mTetrisView.getMode() == TetrisTileView.PAUSE) {
				GlobalVariable.start_time += (System.currentTimeMillis() - GlobalVariable.stop_time);
				GlobalVariable.stop_time = 0;
				mTetrisView.setMode(TetrisTileView.RUNNING);
				imgbtn_play_stop.setImageResource(R.drawable.playing_btn);
				bt_play_stop.setText(getString(R.string.stop));
			}
		} else if (v.getId() == R.id.bt_new_game
				|| v.getId() == R.id.bt_new_game1) {
			GlobalVariable.start_time = System.currentTimeMillis();
			GlobalVariable.stop_time = 0;
			// TetrisTileView.mPlayerCount = 0;
			// TetrisTileView.mFinishedPlayerCount = 0;
			GlobalVariable.current_number_line = 0;
			GlobalVariable.current_score = 0;
			GlobalVariable.level = 1;

			mTetrisView.setMode(TetrisTileView.READY);
			mTetrisView.setMode(TetrisTileView.RUNNING);
			imgbtn_play_stop.setImageResource(R.drawable.playing_btn);
			bt_new_game.setText(R.string.reset);
			bt_play_stop.setText(getString(R.string.stop));
			bt_new_game1.setText(R.string.reset);
		} else if (v.getId() == R.id.bt_menu || v.getId() == R.id.bt_menu1) {
			if (mTetrisView.getMode() == TetrisTileView.RUNNING) {
				GlobalVariable.stop_time = System.currentTimeMillis();
				mTetrisView.setMode(TetrisTileView.PAUSE);
				imgbtn_play_stop.setImageResource(R.drawable.stopping_btn);
			}
			Intent intent = new Intent(this, MenuActivity.class);
			startActivityForResult(intent, Constants.EXITAPP);
		} else if (v.getId() == R.id.bt_exit) {
			finish();
		} else if (v.getId() == R.id.tetris_imgbtn_close_ad) {
//			ad_view.setVisibility(View.GONE);
//			imgbtn_close_ad.setVisibility(View.GONE);
		}
	}

	@Override
	public void onDismiss(DialogInterface arg0) {
		// TODO Auto-generated method stub
		Log.v(TAG, "202 GlobalVariable.isReplay = " + GlobalVariable.isReplay);
		// new game
		if (GlobalVariable.isReplay) {
			GlobalVariable.isReplay = false;
			GlobalVariable.start_time = 0;
			GlobalVariable.stop_time = 0;
			// TetrisTileView.mPlayerCount = 0;
			// TetrisTileView.mFinishedPlayerCount = 0;
			GlobalVariable.current_number_line = 0;
			GlobalVariable.current_score = 0;
			GlobalVariable.level = 1;
			mTetrisView.setMode(TetrisTileView.READY);

			bt_new_game.setText(getString(R.string.start));
			bt_new_game1.setText(getString(R.string.start));
			imgbtn_play_stop.setImageResource(R.drawable.stopping_btn);
			bt_play_stop.setText(R.string.play);
		}
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.imgbtn_down) {
			Log.v(TAG, "onLongClick");
			mTetrisView.navigationUpdate(Navigations.BOTTOM);
			return true;
		}
		return false;
	}
}